# -*- coding: utf-8 -*-
# 默认当前工作路径为 "ns-allinone-3.34/ns-3.34/scratch/"

import xml.etree.ElementTree as ET
import argparse

args = argparse.ArgumentParser()
args.add_argument('-s', '--size',  type=int,
                  help='Indicate the pakcet size of each probe.')
args.add_argument('-f', '--frequency', type=int,
                  help='Indicate the probing frequency.')
args = args.parse_args()

'''
如下为设定 "ns-allinone-3.34/ns-3.34/data/" 数据文件目录；
* 不能在 scratch 目录下生成其他目录，否则， waf 编译时会报错
'''
dataDir = '../data/'

# NS3 仿真的流监控文件; *请注意* 按照仿真程序文件里保存的文件名来对应修改如下待读取的文件名
ns3FlowMon ='data-E2ePathCongestionDetection' + '.flowmon'

pktSize = args.size
probeHz = args.frequency

# 读入流监控文件；其实际上为 xml 格式文件
tree = ET.parse(ns3FlowMon)
root = tree.getroot()
flowStats = root[0]

filename = dataDir + 'PktSize-' + \
    str(pktSize) + '-ProbeHz-' + str(probeHz) + '.txt'
file = open(filename, 'a+')
for fid in range(len(flowStats)):
    flow = flowStats[fid].attrib
    pktSentNum = int(flow['txPackets'])
    pktLostNum = int(flow['lostPackets'])

    pktLossRate = pktLostNum / pktSentNum
    temp_data = [pktLossRate, eval(flow['delaySum'][:-2]) / (
        10**9 * pktSentNum), eval(flow['jitterSum'][:-2]) / (10**9 * pktSentNum)]

    for element in temp_data:
        file.write(str(element)+' ')

    if fid < (len(flowStats) - 1):
        file.write('\n')

file.close()